if(!dojo._hasResource["market.widgets.component.Relationship"]){dojo._hasResource["market.widgets.component.Relationship"]=true;
require(["dojo/_base/declare","market/widgets/CommonWidget","dojo/_base/array","dojo/query","dojo/dom-construct","dojo/dom"],function(declare,CommonWidget,array,query,contruct,dom){
declare("market.widgets.component.Relationship",[CommonWidget],{
_templHtml:'<div><div style="height:150px" id="divMyfriends" class="container">{myFriends}</div><div class="clear"></div></div><div id="divRelationShip"><div style="margin-top:5px;"><div class="sub_menu_title_right" style=""><a href="#" id="hrefAddRelationship">{label1}</a></div></div><div id="divRelatView">{myRealtionships}</div><div id="divRelatForm"></div></div>',
_friendsHtml:'<div class="dojoDndItem"><div class="box_relation_view"><input type="hidden" value="{id}"/><img height="48px" src="getContent/{avatar.contentFile.guid}/small" title="{firstName} {lastName}" alt="{firstName} {lastName}"></div></div>',
_relaHtml:'<div class="box_relation"><span style="color:#00a5db;font-size:11px;font-weight:bold;margin-top:0px;">{relationName}</span><div style="padding:1px;background-color:#00A5DB;float:right;font-size:12px;font-weight:bold;"><a href="#"><input type="hidden" value="{id}">Xoa</a></div><div class="box_relation_content container" id="relation_{id}"><input type="hidden" value="{id}">{myfriends}</div></div>',
_CREATE_FORM:"relationship.addForm",_currentForm:null,_dataSource:{},
show:function(){
   dojo.require("dojo.dnd.Source");
   var _r=this;
   var args = {ajaxUrl:'participant/relationship/list',contentId:this.contentId,callback:function(data){
      _r._init(data);
   }};
   SYSTEMWIDGET.ajaxWidget(args).callXhrGetJson();
},
_init:function(data){
   this._parseLayout(data);
   this._initEvents();
   var _r=this;
   query('#hrefAddRelationship',this.contentId).onclick(function(){
      _r.showCreateForm();
   });
   var friendSource = new dojo.dnd.Source("divMyfriends",{copyOnly:"true"});
   friendSource.checkAcceptance = function(){
      return false;
   };
   var reSource = new dojo.dnd.Source("divRelationShip");
   reSource.checkAcceptance = function(source,nodes){
      if (source.node.id=='divMyfriends') return false;
      return true;
   };
   var objs=query('#divRelatView div.box_relation_content');
   this._initDnd(objs);
   dojo.subscribe("/dnd/drop/before",function(source,nodes,copy,target){
      var targetSource=null;
      var targetNode=target.node;
      var friendId=query('input[type="hidden"]',nodes[0])[0].value;
      var ajaxArgs={};
      var relationId=null;
      var targetId=targetNode.id;
      if (targetId=='divRelationShip'){
         targetSource=_r._dataSource[source.node.id];
         relationId=query('input[type="hidden"]',source.node)[0].value;
         ajaxArgs.ajaxUrl='participant/relationship/remove/friend';
         ajaxArgs.callback=function(){
            targetSource[friendId]=null;
         };
      }else{
         targetSource=_r._dataSource[targetId];
         relationId=query('input[type="hidden"]',targetNode)[0].value;
         ajaxArgs.ajaxUrl='participant/relationship/add/friend';
         ajaxArgs.callback=function(){
            targetSource[friendId]=friendId;
         };
      }
      ajaxArgs.postData='relationshipId='+relationId+'&friendId='+friendId;
      ajaxArgs.contentId=targetNode.id;
      SYSTEMWIDGET.ajaxWidget(ajaxArgs).callXhrPostJSON();
   });
   dojo.subscribe("/dnd/drop",function(source,nodes,copy,target){
      if (target.node.id=='divRelationShip'){
         reSource.clearItems();
         reSource.deleteSelectedNodes();
      };
   });
},
_initEvents:function(){
   _r=this;
   query('div.box_relation a','divRelatView').onclick(function(){
      var currentNode=this;
      SYSTEMWIDGET.ajaxWidget({ajaxUrl:'participant/relationship/remove',contentId:'divRelatView',
         postData:'relationshipId='+query('input[type="hidden"]',this)[0].value,
         callback:function(){
            contruct.destroy(currentNode.parentNode.parentNode); 
         }}).callXhrPostJSON();
   });
},
_initDnd:function(objs){
   var _r=this;
   for(var index=0;index<objs.length;index++){
      new dojo.dnd.Source(objs[index],{checkAcceptance:function(source,nodes){
         if (source.node.id=='divMyfriends'){
            var targetSource=_r._dataSource[this.node.id];
            var friendId=query('input[type="hidden"]',nodes[0])[0].value;
            if (targetSource[friendId]) return false;
            else return true;
            return true;
         }
         return false;
      }});
   }
},
_parseLayout:function(datasource){
   this._dataSource={};
   var friendsHTML=this._parseFriendsHtml(datasource.friends);
   var relations=datasource.relationships;
   var relaHTML='';
   for(var i=0;i<relations.length;i++){
      var relation=relations[i];
      var relaFriends=relation.friends;
      var relaFriendsHTML=this._parseFriendsHtml(relaFriends,true,relation.id);
      relation.myfriends=relaFriendsHTML;
      relaHTML+=dojo.replace(this._relaHtml,relation);
   }
   dojo.html.set(dom.byId(this.contentId),dojo.replace(this._templHtml,{label1:datasource.label1,myFriends:friendsHTML,myRealtionships:relaHTML}),{parseContent: false});
},
_parseFriendsHtml:function(friends,hasPart,relationId){
   var friendsHTML='';
   var targetSource=null;
   if (relationId){
      var nodeId="relation_"+relationId;
      targetSource=this._dataSource[nodeId];
      if (!targetSource){
         targetSource={};
         this._dataSource[nodeId]=targetSource;
      }
   }
   for(var i=0;i<friends.length;i++){
      var friend=hasPart?friends[i].participant:friends[i];
      if(targetSource) targetSource[friend.id]=friend.id;
      friendsHTML+=dojo.replace(this._friendsHtml,friend);
   }
   return friendsHTML;
},
showCreateForm:function(){
   this._visibleView(false);
   this._currentForm=this.createForm(this._CREATE_FORM,'divRelatForm','participant/relationship/add',{},{click:this._handleClickEvents()},null,true);
},
_handleClickEvents:function(){
   _r=this;
   var clickEvents = {
      events:['btnCancel','btnAdd'],
      btnCancel:function(data){ _r._visibleView(true); },
      btnAdd:function(data){ _r._add(); }
   };
   return clickEvents;
},
_visibleView:function(visible){
   dojo.byId("divRelatView").style.display=visible?"inline":"none";
   dojo.byId("divRelatForm").style.display=visible?"none":"inline";
},
_insertRela:function(relation){
   relation.myfriends='';
   var relaHTML=dojo.replace(this._relaHtml,relation);
   var _div=contruct.create('div',{innerHTML:relaHTML},'divRelatView','last');
   var objs=query('div.box_relation_content',_div);
   this._initDnd(objs);
   this._dataSource["relation_"+relation.id]={};
   this._visibleView(true);
   this._initEvents();
},
_add:function(){
   _r=this;
   var handleSuccess=function(data){
      _r._insertRela(data);
      SYSTEMWIDGET.messageWidget().showSucc(data.messageSucc);
   };
   this._currentForm.callSaveForm({handleSuccess:handleSuccess});
}
});});}
